export class RateSchedule {
    private fn: () => void;
    private to: number;
    private step: number;
    constructor(fn: () => void, from: number, to: number, step: number) {
        this.init(fn, from, to, step);
    }
    private init(fn: () => void, from: number, to: number, step: number) {
        this.fn = fn;
        this.step = step;
        this.to = to * 1000;
        this.currentInterval = from * 1000;
    }

    private timeID: number = 0;
    private isStart: boolean = false;
    private exit: boolean = false;
    private currentInterval: number;
    public start(): void {
        if (this.isStart) return;
        this.exit = false;
        this.isStart = true;
        this.timeID = setInterval(() => {
            if (this.currentInterval + this.step > this.to) {
                this.currentInterval = this.to;
            } else {
                this.currentInterval += this.step;
            }
        }, 1000);
        this.callF();
    }
    public cancle(): void {
        this.exit = true;
        this.isStart = false;
        clearInterval(this.timeID);
    }
    private callF(): void {
        if (this.exit) return;
        setTimeout(() => {
            this.fn();
            this.callF();
        }, this.currentInterval);
    }
}
